/**
 * 
 */
package com.smartdio.company.editor;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

import com.smartdio.company.Activator;
import com.smartdio.company.Messages;
import com.smartdio.company.extention.IPersonEditor;
import com.smartdio.company.model.Person;



/**
 * @author mai
 * 
 */
public class PersonInfoPanel extends Composite {

	private PersonBasicPanel personnelBasicPanel = null;

	private PersonPersonPanel personnelPersonPanel = null;

	private PersonHomePanel personnelHomePanel = null;

	private PersonWorkPanel personnelWorkPanel = null;

	private TabFolder tabFolder = null;
	
	private IPersonEditor[] extentEditors  ;

	public PersonInfoPanel(Composite parent, int style) {
		super(parent, style);
		initialize();
	}
	
	private void initialize() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		createTabFolder();
		this.setLayout(gridLayout);

		initExtentionEditor();
		//自动对字符串进行国际化翻译
		Messages.i18nWidgetWithChilds(this);
	}

	/**
	 * This method initializes personnelBasicPanel
	 * 
	 */
	private void createPersonnelBasicPanel() {
		personnelBasicPanel = new PersonBasicPanel(tabFolder, SWT.NONE);

	}

	/**
	 * This method initializes personnelPersonPanel
	 * 
	 */
	private void createPersonnelPersonPanel() {
		personnelPersonPanel = new PersonPersonPanel(tabFolder, SWT.NONE);
	}

	/**
	 * This method initializes personnelHomePanel
	 * 
	 */
	private void createPersonnelHomePanel() {
		personnelHomePanel = new PersonHomePanel(tabFolder, SWT.NONE);
	}

	/**
	 * This method initializes personnelWorkPanel
	 * 
	 */
	private void createPersonnelWorkPanel() {
		personnelWorkPanel = new PersonWorkPanel(tabFolder, SWT.NONE);
	}

	/**
	 * This method initializes tabFolder
	 * 
	 */
	private void createTabFolder() {
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData.grabExcessVerticalSpace = true;
		tabFolder = new TabFolder(this, SWT.NONE);

		createPersonnelBasicPanel();
		tabFolder.setLayoutData(gridData);

		createPersonnelPersonPanel();
		TabItem tabItem1 = new TabItem(tabFolder, SWT.NONE);
		tabItem1.setText("%personnel");
		tabItem1.setControl(personnelPersonPanel);

		createPersonnelWorkPanel();
		TabItem tabItem2 = new TabItem(tabFolder, SWT.NONE);
		tabItem2.setText("%work");
		tabItem2.setControl(personnelWorkPanel);

		createPersonnelHomePanel();
		TabItem tabItem3 = new TabItem(tabFolder, SWT.NONE);
		tabItem3.setText("%home");

		TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
		tabItem.setText("%basic");
		tabItem.setControl(personnelBasicPanel);
		tabItem3.setControl(personnelHomePanel);


	}


	public void setValue(Person person){
		personnelBasicPanel.setValue(person);
		personnelPersonPanel.setValue(person);
		personnelHomePanel.setValue(person);
		personnelWorkPanel.setValue(person);
		for(IPersonEditor editor:extentEditors){
			if(editor!=null){
				editor.putPersonValue(person);
			}
		}
	}

	public void addModifyListener(ModifyListener ls){
		personnelBasicPanel.addModifyListener(ls);
		personnelPersonPanel.addModifyListener(ls);
		personnelHomePanel.addModifyListener(ls);
		personnelWorkPanel.addModifyListener(ls);
	}
	
	/**
	 * 初始化扩展的编辑器
	 */
	private void initExtentionEditor(){
		// 通过Activator中的方法获取扩展的设置
		IConfigurationElement[] els = Activator.getDefault().getPersonEditors();
		extentEditors = new IPersonEditor[els.length];
		// 根据每个扩展设置建立编辑控件
		for(int i =0;i<els.length;i++  ){
			IPersonEditor editor =createExtentionEditor(els[i]);
			extentEditors[i]=editor;
			
		}
		
	}
	/**
	 * 根据单个扩展设置建立IPersonEditor对象
	 * @param ele  IConfigurationElement对象
	 * @return  返回IPersonEditor对象
	 */
	private IPersonEditor createExtentionEditor(IConfigurationElement ele){
		String id = ele.getAttribute("id");
		String name =ele.getAttribute("name");
		try {
			// createExecutableExtension方法用于把Java类型的属性变成对象实例
			IPersonEditor editor = (IPersonEditor)ele.createExecutableExtension("class");
			
			TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
			tabItem.setText(name);
			Control control = editor.createPartControl(tabFolder);
			tabItem.setControl(control);
			return editor;
		} catch (CoreException e) {
			e.printStackTrace();
			return null;
		}
		
	}
}
